using System.Linq;
using CMS.Application.common.Interface;
using CMS.Application.Common.Interface;
using CMS.Domain.Entity;
using SoftDemo.Api.Controllers;

namespace CMS.Api.Controllers
{



    public class Sortpaging
    {

       
        /// <summary>
        /// 分页静态类
        /// </summary>
        public async Task<Array> SortPaging(IOrderedQueryable<Blog> blogs , int _page , IFileUploadService  _fileUploadService ,IRepository<Users> _Users,IRepository<BlogCollect> _BlogCollect,IRepository<Comment> _Comment)
        {

            var GetImgClass = new GetImg();

          
            // 按页数截取片段
            var newblogs = blogs.Skip((_page-1) * 5).Take(5).ToArray();

            // 新建一个数组用于返回数据
            object[] endBlogs = new object[newblogs.Length];

            int i = 0;

            // 循环添加我们的数据
            foreach (var item in newblogs)
            {

                // 获得头像
                var userAvatar = await GetImgClass.GetImgFn(item.AuthorAvatar, _fileUploadService);

                // 获得姓名
                var userData = await _Users.GetByIdAsync(item.AuthorId);

                // 获赞个数
                var collectCount = _BlogCollect.Table.Where(x => x.BlogId == item.BlogId).ToList().Count();

                // 获评个数
                var commentCount = _Comment.Table.Where(x => x.BlogId == item.BlogId).ToList().Count();
               
               
                var data = new
                {

                    item,

                    userData.UserName,

                    userAvatar,

                    collectCount,

                    commentCount

                };
                
                endBlogs[i] = data;

                i+=1;

            }

            return endBlogs;
        }

    }
}